package cn.zhxu.research;

import cn.zhxu.toys.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class DataItem {

    public static final int SCORE_MAX = 5;

    private String txt1;
    private String txt2;
    private int score;
    private float similarity;
    private int mScore; // 模型计算出的额相似度等级

    public DataItem(String txt1, String txt2, int score) {
        this.txt1 = txt1;
        this.txt2 = txt2;
        this.score = score;
    }

    public DataItem(String txt1, String txt2, int score, float similarity) {
        this.txt1 = txt1;
        this.txt2 = txt2;
        this.score = score;
        this.similarity = similarity;
    }

    public static List<DataItem> readDataItems(String path) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
            List<DataItem> items = new ArrayList<>();
            String line = reader.readLine();
            while (StringUtils.notBlank(line)) {
                String[] split = line.split("##");
                if (split.length == 3) {
                    var str = split[2];
                    var idx = str.indexOf(',');
                    if (idx > 0) {
                        var score = Integer.parseInt(str.substring(0, idx));
                        var similarity = Float.parseFloat(str.substring(idx + 1));
                        items.add(new DataItem(split[0], split[1], score, similarity));
                    } else {
                        items.add(new DataItem(split[0], split[1], Integer.parseInt(str)));
                    }
                }
                line = reader.readLine();
            }
            return items;
        }
    }

    public String toString() {
        return txt1 + "##" + txt2 + "##" + score;
    }

}
